﻿@using viBook.Models
@model IEnumerable<ReportOnBook>
@{
    ViewBag.Title = "Book Report";
    ViewBag.Menu = "Report";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    
}
@(Html.Telerik().Grid(Model)
               .Name("report-book")
               .DataKeys(keys => keys.Add(r => r.Id))
               .Columns(columns =>
                            {
                                columns.Bound(r => r.Id).Width(50);
                                columns.Bound(r => r.ReportedDate).Format("{0:dd/MM/yyyy}").Width(120);
                                columns.Bound(r => r.Book.Title)
                                    .Template(
                                            @<text><a href='@Url.Action("Details", "Books", new {id = item.BookId})'>@item.Book.Title</a>
                                            </text>
                                    )
                                    .ClientTemplate(string.Format("<a href='/Books/Details/<#= Book.Id #>'><#= Book.Title #></a>"))
                                    .Title("Book")
                                    .Width(250);
                                columns.Bound(r => r.Id)
                                    .Template(
                                        @<text>
                                             <a href='javascript:approve(@item.Id)' class="t-button" >Approve</a>
                                             <a href='javascript:disapprove(@item.Id)' class="t-button" >Disapprove</a>
                                         </text>
                                    )
                                    .ClientTemplate(
                                        string.Format("<a class='t-button' href='javascript:approve(<#= Id #>)'>Approve</a>" +
                                                      "<a class='t-button' href='javascript:disapprove(<#= Id #>)'>Disapprove</a>")
                                    )
                                    .Title("Action")
                                    .Width(140);
                            })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Server().Select("BooksReport", "Admins");
                    dataBinding.Ajax().Select("SelectBookReportAjax", "Admins").Enabled(true);
                })
                .Scrollable()
                .Sortable()
                .Pageable()
                .Filterable()
                .Footer(true)
        )
<script type="text/javascript">
    function approve(id) {
        $.ajax({
            url: '@Url.Action("ApproveBookReportAjax", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function (result) {
                var reportBookGrid = $('#report-book').data('tGrid');
                reportBookGrid.rebind();
            }
        });
    }

    function disapprove(id) {
        $.ajax({
            url: '@Url.Action("DisapproveBookReportAjax", "Admins")',
            type: 'POST',
            data: "{ id:" + id + "}",
            contentType: 'APPLICATION/JSON',
            success: function (result) {
                var reportBookGrid = $('#report-book').data('tGrid');
                reportBookGrid.rebind();
            }
        });
    }
</script>